package com.zgt.dao.impl;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.sun.xml.internal.org.jvnet.fastinfoset.RestrictedAlphabet;
import com.zgt.dao.TeacherDao;
import com.zgt.model.Admin;
import com.zgt.model.Teacher;

@Repository("teacherDao")
@SuppressWarnings("unchecked")
public class TeacherDaoImpl implements TeacherDao {

	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void deleteTeacherById(int id) {
		Teacher teacher = new Teacher();
		teacher.setId(id);
		hibernateTemplate.delete(teacher);
	}

	public Teacher getTeacherById(int id) {
		return hibernateTemplate.get(Teacher.class, id);
	}

	public void saveTeacher(Teacher teacher) {
		hibernateTemplate.save(teacher);
	}

	public void updateTeacher(Teacher teacher) {
		hibernateTemplate.update(teacher);
	}

	public Teacher checkLogin(Teacher teacher) {
		Teacher t = null;
		List<Teacher> teachers = hibernateTemplate
				.findByCriteria(DetachedCriteria.forClass(Teacher.class).add(
						Restrictions.eq("workNum", teacher.getWorkNum())).add(
						Restrictions.eq("password", teacher.getPassword())));
		Iterator<Teacher> it = teachers.iterator();
		while (it.hasNext()) {
			t = it.next();
		}
		return t;
	}

	public boolean isExist(Teacher Teacher) {
		return false;
	}

	public int getCount(String query) {
		List<Teacher> teachers = hibernateTemplate.find(query);
		return teachers.size();
	}

	public List<Teacher> listTeacher(int pageStart, int pageSize) {
		List<Teacher> teachers = hibernateTemplate.findByCriteria(
				DetachedCriteria.forClass(Teacher.class), pageStart, pageSize);
		return teachers;
	}

	public void saveTeachers(List list) {
		Iterator it = list.iterator();
		while (it.hasNext()) {
			hibernateTemplate.save(it.next());
		}
	}

	public List<Teacher> listSearch(final String query, final int start,
			final int size) {
		List<Teacher> teachers = hibernateTemplate
				.executeFind(new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						return session.createQuery(query).setFirstResult(start)
								.setMaxResults(size).list();
					}
				});
		return teachers;
	}

	public List<Teacher> toExcle(final String keyType, final String key) {
		List<Teacher> teachers = hibernateTemplate
				.executeFind(new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Criteria query = session.createCriteria(Teacher.class);
						if (!key.isEmpty()) {
							query.add(Restrictions.like(keyType, "%" + key
									+ "%"));
						}
						return query.list();
					}
				});
		return teachers;
	}

}
